﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace BackupLib.Remoting
{
    public class ServerMethods : MarshalByRefObject, IBackupTask
    {
        //public event EventHandler<BackupEventArgs> OnStop;

        public event EventHandler<BackupFileEventArgs> OnFile;
        public event EventHandler<BackupEventArgs> OnStart;
        public event EventHandler<BackupFinishedEventArgs> OnStop;

        public ServerMethods()
        {
            ServiceSettings.OnFile += new EventHandler<BackupFileEventArgs>(ServiceSettings_OnFile);
            ServiceSettings.OnStart += new EventHandler<BackupEventArgs>(ServiceSettings_OnStart);
            ServiceSettings.OnStop += new EventHandler<BackupFinishedEventArgs>(ServiceSettings_OnStop);
        }

        ObservableCollection<string> IBackupTask.DoBackup()
        {
            ServiceSettings.NextExecution = DateTime.Now;
            return null;
        }

        void ServiceSettings_OnStop(object sender, BackupFinishedEventArgs e)
        {
            if (OnStop != null)
                OnStop(null, e);
        }

        void ServiceSettings_OnStart(object sender, BackupEventArgs e)
        {
            if (OnStart != null)
                OnStart(null, e);
        }

        void ServiceSettings_OnFile(object sender, BackupFileEventArgs e)
        {
            if (OnFile != null)
                OnFile(null, e);
        }

        public void ReloadProject()
        {
            ServiceSettings.BackupProject = XML.Deserialize();
        }

        public void Pause()
        {
            ServiceSettings.BackupProject.Pause();
        }

        public void Stop()
        {
            ServiceSettings.BackupProject.Stop();
        }

        public bool IsProcessing
        {
            get { return ServiceSettings.IsProcessing; }
        }
    }
}
